package com.haoshuang.zhouzhoubang.ui.page.mine.integral.detail

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityStoreIntegralDetailBinding
import com.haoshuang.zhouzhoubang.model.bean.YuBean
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.adapter
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.vertical


/**
 *  门店积分明细
 */
class StoreIntegralDetailActivity :
    BaseVMActivity<StoreIntegralDetailViewModel, ActivityStoreIntegralDetailBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<StoreIntegralDetailActivity>()
        }
    }

    private val data = arrayListOf<YuBean.Data>()
    private val adapter = StoreIntegralDetailAdapter(data)

    // 积分明细
    var type = 4
    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "门店积分明细"
        binding.titleBar.leftImageView().click { finish() }

        binding.recyclerView.vertical()
            .adapter(adapter)

        adapter.setEmptyData()
        adapter.setOnItemClick { view, position, item ->
            if (item == null) return@setOnItemClick
            if (item.type == "team_refund_deposit") return@setOnItemClick
            if (item.link_id != null && item.link_id != "0" && item.link_id.isNotEmpty()) {
                // todo 跳转订单详情
//                startPage<ServiceOrderStatusActivity>("ids" to item.link_id.toIntOrNull())
            }
        }

    }

    override fun initData() {
        binding.refreshLayout.setOnRefreshListener {
            p = 1
            getNet()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            p++
            getNet()
        }
        p = 1
        getNet()
    }

    private fun getNet() {
        vm.getData("page" to p, "limit" to 10, "type" to 6)
    }

    override fun startObserve() {
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it?.data, data, adapter, p)
            }
        }
    }

}